<?php

namespace App\Admin\Controllers;

use App\Models\Cards;
use App\Models\CardSeries;
use App\Models\UserDrawLotsLog;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CardSeriesController extends Controller
{
    use ModelForm;

    public function index(){
        return Admin::content(function (Content $content) {

            $content->header('卡片系列管理');

            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑卡片系列');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建卡片系列');

            $content->body($this->form());
        });
    }



    protected function grid(){
        return Admin::grid(CardSeries::class, function (Grid $grid) {

            $grid->model()->orderByDesc('id');

            $grid->name('系列名称');
            $grid->descr('描述');
            $grid->img('图片')->image('',50,50);
            $grid->cards('卡片数量')->display(function($cards){
                return count($this->cards);
            });
            $grid->is_open('是否开启')->switch();
            $grid->actions(function($actions){
                $actions->prepend('<a href="'.'awards/'.$actions->getKey().'/users'.'" title="查看用户领取记录"> <i class="fa fa-users"></i></a>');
                $actions->prepend('<a href="'.'awards/'.$actions->getKey().'/lists'.'" title="查看奖励"> <i class="fa fa-gift"></i></a>');
                $actions->prepend('<a href="'.'card_series/'.$actions->getKey().'/cards'.'" title="查看卡片"><i class="fa fa-bars"></i></a>');
            });

        });
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(CardSeries::class, function (Form $form) {

            $form->hidden('id', 'ID');

            $form->text('name','系列名称')->rules('required');
            $form->textarea('descr','系列描述')->rules('required');
            $form->editor('rule','系列规则');
            $form->image('img','展位图')->rules('required');
            $form->image('first_img','卡套首图');
            $form->switch('is_open','是否开启')->default(1);
        });
    }

    public function cardSeriesList(){
        $data = CardSeries::all()->toArray();
        if(!$data) return [];
        $arr = array();

        foreach ($data as $d){
            $v['id']=$d['id'];
            $v['text'] = $d['name'];
            $arr[] = $v;
        }
        return $arr;
    }
    
    public function destroy($id)
    {
        //删除前的判断
        if(Cards::where('card_series_id',$id)->exists()){
            return response()->json([
                'status'  => false,
                'message' => '删除失败，还有相关卡片未删除',
            ]);
        }
        if ($this->form()->destroy($id)) {
            UserDrawLotsLog::where('card_series_id',$id)->update(['card_id'=>0,'card_series_id'=>0]);
            Cards::where('card_series_id',$id)->delete();
            return response()->json([
                'status'  => true,
                'message' => trans('admin.delete_succeeded').'已删除相关记录',
            ]);
        } else {
            return response()->json([
                'status'  => false,
                'message' => trans('admin.delete_failed'),
            ]);
        }
    }
}
